home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / TEST_STA.C < prev    next >
C/C++ Source or Header  |  1992-09-23  |  7KB  |  196 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12.  
  13. #include <cool/String.h>
  14. #include <cool/Stack.h>
  15. #include <cool/Stack.C>
  16. #include <test.h>
  17.  
  18. typedef char* charP;
  19.  
  20. Boolean my_compare_charP (const charP& s1, const charP& s2) {
  21.   return (strcmp (s1, s2) ? FALSE : TRUE);
  22. }
  23.  
  24. void test_int () {
  25.   CoolStack<int> s0;
  26.   TEST("CoolStack<int> s0", 1, 1);
  27.   CoolStack<int> s1(4);
  28.   TEST("CoolStack<int> s1", 1, 1);
  29.   CoolStack<int> s2(4);
  30.   s1.pushn(7, 4);
  31.   TEST("s1.push(7, 4)", (s1[3]==7 && s1[2]==7 && s1[1]==7 && s1[0]==7), TRUE);
  32.   TEST("s1.length()", s1.length(), 4);
  33.   s2.push(7);
  34.   TEST("s2.push(7)", (s2[0]==7), TRUE);
  35.   s2.push(7);
  36.   TEST("s2.push(7)", (s2[0]==7), TRUE);
  37.   s2.pushn(7, 2);
  38.   TEST("s2.pushn(7, 2)", 1, 1);
  39.   TEST("s1.operator==(s2)", s1.operator==(s2), TRUE);
  40.   TEST("s2.operator!=(s1)", s2.operator!=(s1), FALSE);
  41.   s2.pop();
  42.   TEST("s2.pop()", (s2.length()==3 && s2[0]==7), TRUE);
  43.   s2.popn(2);
  44.   TEST("s2.popn(2)", (s2.length()==1 && s2[0]==7), TRUE);
  45.   s2.pushn(1, 3);  
  46.   TEST("s2.pushn(1, 3)", 1, 1);
  47.   TEST("s1.operator=(s2)", s1.operator=(s2), s2);
  48.   TEST("s1.top()", (s1.top() == 1), TRUE);
  49.   TEST("s2.position(7)", s2.position(7), 3);
  50.   TEST("s1.find(33)", s1.find(33), FALSE);
  51.   TEST("s2.operator[](3)", s2.operator[](3), 7);
  52.   s2.clear();
  53.   TEST("s2.clear()", 1, 1);
  54.   TEST("s2.is_empty()", s2.is_empty(), TRUE);
  55.   s2.resize(8);
  56.   TEST("s2.resize(8)", 1, 1);
  57.   TEST("s1.pushn(4, 8)", s1.pushn(4, 8), TRUE);
  58.   TEST("s1.popn(13)", s1.popn(13), 7);
  59.   s1.set_length(10);
  60.   TEST("s1.set_length(10)", s1.length(), 10);
  61.   s1.set_growth_ratio(2.0);
  62.   TEST("s1.set_growth_ratio(2.0)", 1, 1);
  63.   s1.set_alloc_size(50);
  64.   TEST("s1.set_alloc_size(50)", 1, 1);
  65.   CoolStack<int> s3(s1);
  66.   TEST("CoolStack<int> s3 = s1", (s3==s1), TRUE);
  67. }
  68.  
  69. void test_charP() {
  70.   CoolStack<char*> s0;
  71.   TEST("CoolStack<char*> s0", 1, 1);
  72.   TEST("s0.length()", s0.length(), 0);
  73.   CoolStack<char*> s1(4);
  74.   TEST("CoolStack<char*> s1(4)", s1.capacity(), 4);
  75.   CoolStack<char*> s2(4);
  76.   TEST("CoolStack<char*> s2(4)", s1.capacity(), 4);
  77.   s1.set_compare(&my_compare_charP);
  78.   TEST ("s1.set_compare(&my_compare_charP)", 1,1);
  79.   s1.pushn("AAA", 4);
  80.   TEST("s1.pushn(\"AAA\", 4)", ((strcmp (s1[3],"AAA")==0) && (strcmp (s1[2],"AAA")==0) && (strcmp (s1[1],"AAA")==0) && (strcmp (s1[0],"AAA")==0)), 1);
  81.   TEST("s1.length()", s1.length(), 4);
  82.   s2.push("AAA");
  83.   TEST("s2.push(\"AAA\")", (strcmp (s2[0],"AAA")==0), 1);
  84.   TEST("s2.length()", s2.length(), 1);
  85.   s2.push("AAA");
  86.   TEST("s2.push(\"AAA\")", (strcmp (s2[0],"AAA")==0), 1);
  87.   TEST("s2.length()", s2.length(), 2);
  88.   s2.pushn("AAA", 2);
  89.   TEST("s2.pushn(\"AAA\", 2)", 1, 1);
  90.   TEST("s2.length()", s2.length(), 4);
  91.   TEST("s1.operator==(s2)", s1.operator==(s2), TRUE);
  92.   TEST("s2.operator!=(s1)", s2.operator!=(s1), FALSE);
  93.   s2.pop();
  94.   TEST("s2.pop()", (s2.length()==3 && (strcmp (s2[0], "AAA")==0)), 1);
  95.   s2.popn(2);
  96.   TEST("s2.popn(2)", (s2.length()==1 && (strcmp (s2[0], "AAA")==0)), 1);
  97.   s2.pushn("BBB", 3);  
  98.   TEST("s2.pushn(\"BBB\", 3)", 1, 1);
  99.   TEST("s1.operator=(s2)", s1.operator=(s2), s2);
  100.   TEST("s1.top()", (strcmp(s1.top(),"BBB")), 0);
  101.   TEST("s2.position(\"AAA\")", s2.position("AAA"), 3);
  102.   TEST("s1.find(\"ZZZ\")", s1.find("ZZZ"), FALSE);
  103.   TEST("s2.operator[](3)", (strcmp (s2.operator[](3), "AAA")), 0);
  104.   s2.clear();
  105.   TEST("s2.clear()", 1, 1);
  106.   TEST ("s2.length()", s2.length(), 0);
  107.   TEST("s2.is_empty()", s2.is_empty(), TRUE);
  108.   s2.resize(8);
  109.   TEST("s2.resize(8)", 1, 1);
  110.   TEST ("s2.capacity()", s2.capacity(), 8);
  111.   TEST("s1.pushn(\"CCC\", 8)", s1.pushn("CCC", 8), TRUE);
  112.   TEST("s1.popn(13)", strcmp (s1.popn(13), "AAA"), 0);
  113.   s1.set_length(10);
  114.   TEST("s1.set_length(10)", s1.length(), 10);
  115.   s1.set_growth_ratio(2.0);
  116.   TEST("s1.set_growth_ratio(2.0)", 1, 1);
  117.   s1.set_alloc_size(50);
  118.   TEST("s1.set_alloc_size(50)", 1, 1);
  119.   CoolStack<char*> s3(s1);
  120.   TEST("CoolStack<char*> s3 = s1", (s3==s1), TRUE);
  121. }
  122.  
  123. void test_String () {
  124.   CoolString S1("AAA");
  125.   CoolStack<CoolString> s0;
  126.   TEST("CoolStack<CoolString> s0", 1, 1);
  127.   TEST("s0.length()", s0.length(), 0);
  128.   CoolStack<CoolString> s1(4);
  129.   TEST("CoolStack<CoolString> s1(4)", s1.capacity(), 4);
  130.   CoolStack<CoolString> s2(4);
  131.   TEST("CoolStack<CoolString> s2(4)", s1.capacity(), 4);
  132.   s1.pushn(CoolString("AAA"), 4);
  133.   TEST("s1.pushn(CoolString(\"AAA\"), 4)", (s1[3]==S1 && s1[2]==S1 && s1[1]==S1 && s1[0]==S1), TRUE);
  134.   TEST("s1.length()", s1.length(), 4);
  135.   s2.push(CoolString("AAA"));
  136.   TEST("s2.push(CoolString(\"AAA\"))", s2[0]==S1, TRUE);
  137.   TEST("s2.length()", s2.length(), 1);
  138.   s2.push(CoolString("AAA"));
  139.   TEST("s2.push(CoolString(\"AAA\"))", s2[0]==S1, TRUE);
  140.   TEST("s2.length()", s2.length(), 2);
  141.   s2.pushn(CoolString("AAA"), 2);
  142.   TEST("s2.pushn(CoolString(\"AAA\"), 2)", 1, 1);
  143.   TEST("s2.length()", s2.length(), 4);
  144.   TEST("s1.operator==(s2)", s1.operator==(s2), TRUE);
  145.   TEST("s2.operator!=(s1)", s2.operator!=(s1), FALSE);
  146.   s2.pop();
  147.   TEST("s2.pop()", (s2.length()==3 && s2[0] == S1), 1);
  148.   s2.popn(2);
  149.   TEST("s2.popn(2)", (s2.length()==1 && s2[0] == S1), 1);
  150.   s2.pushn(CoolString("BBB"), 3);  
  151.   TEST("s2.pushn(CoolString(\"BBB\"), 3)", 1, 1);
  152.   TEST("s1.operator=(s2)", s1.operator=(s2), s2);
  153.   TEST("s1.top()", (strcmp(s1.top(),CoolString("BBB"))), 0);
  154.   TEST("s2.position(CoolString(\"AAA\"))", s2.position(CoolString("AAA")), 3);
  155.   TEST("s1.find(CoolString(\"ZZZ\"))", s1.find(CoolString("ZZZ")), FALSE);
  156.   TEST("s2.operator[](3)", (strcmp (s2.operator[](3), CoolString("AAA"))), 0);
  157.   s2.clear();
  158.   TEST("s2.clear()", 1, 1);
  159.   TEST ("s2.length()", s2.length(), 0);
  160.   TEST("s2.is_empty()", s2.is_empty(), TRUE);
  161.   s2.resize(8);
  162.   TEST("s2.resize(8)", 1, 1);
  163.   TEST ("s2.capacity()", s2.capacity(), 8);
  164.   TEST("s1.pushn(CoolString(\"CCC\"), 8)", s1.pushn(CoolString("CCC"), 8), TRUE);
  165.   TEST("s1.popn(13)", strcmp (s1.popn(13), CoolString("AAA")), 0);
  166.   s1.set_length(10);
  167.   TEST("s1.set_length(10)", s1.length(), 10);
  168.   s1.set_growth_ratio(2.0);
  169.   TEST("s1.set_growth_ratio(2.0)", 1, 1);
  170.   s1.set_alloc_size(50);
  171.   TEST("s1.set_alloc_size(50)", 1, 1);
  172.   CoolStack<CoolString> s3(s1);
  173.   TEST("CoolStack<CoolString> s3 = s1", (s3==s1), TRUE);
  174. }
  175.  
  176. void test_leak () {
  177.   for (;;) {
  178.   test_int();
  179.   test_charP();
  180.   test_String();
  181.   }
  182. }
  183.  
  184. int main (void) {
  185.   START("CoolStack");
  186.   test_int();
  187.   test_charP();
  188.   test_String();
  189. #if LEAK
  190.   test_leak();
  191. #endif
  192.   SUMMARY();
  193.   return 0;
  194. }
  195.  
  196.